package com.hbnu.common.dao.impl;

import com.hbnu.common.dao.UserDao;
import com.hbnu.common.entity.User;
import com.hbnu.common.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * {@link UserDao} 接口的实现类，负责处理与 `user` 表相关的数据库操作。
 * 依赖 {@link DBUtil} 工具类获取数据库连接，并使用 try-with-resources 确保资源自动关闭。
 */

public class UserDaoImpl implements UserDao {

    /**
     * {@inheritDoc}
     */
    @Override
    public void addUser(String username, String password) throws SQLException {
        addUser(username, password, null);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public User queryUser(String username) throws SQLException {
        String sql = "select username, password, nickname from user where username = ?";

        try (Connection conn = DBUtil.getConnection();
        PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, username);

            try (ResultSet rs = pstmt.executeQuery()) {
                if (rs.next()) {
                    return new User(
                            rs.getString("username"),
                            rs.getString("password"),
                            rs.getString("nickname")
                    );
                } else {
                    return null;
                }
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void updatePassword(String username, String newPassword) throws SQLException {
        String sql = "update user set password = ? where username = ?";

        try (Connection conn = DBUtil.getConnection();
        PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, newPassword);
            pstmt.setString(2, username);

            if (pstmt.executeUpdate() < 1) {
                throw new SQLException("UserDao.updatePassword: pstmt.executeUpdate() < 1");
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void addUser(String username, String password, String nickName) throws SQLException {
        String sql = "insert into user(username, password, nickname) values(?, ?, ?)";

        try (Connection conn = DBUtil.getConnection();
        PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, username);
            pstmt.setString(2, password);
            pstmt.setString(3, nickName);

            if (pstmt.executeUpdate() < 1) {
                throw new SQLException("UserDao.addUser: pstmt.executeUpdate() < 1");
            }
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void updateNickname(String username, String nickname) throws SQLException {
        String sql = "update user set nickname = ? where username = ?";

        try (Connection conn = DBUtil.getConnection();
        PreparedStatement pstmt = conn.prepareStatement(sql)) {
            pstmt.setString(1, nickname);
            pstmt.setString(2, username);

            if (pstmt.executeUpdate() < 1) {
                throw new SQLException("UserDao.updateNickname: pstmt.executeUpdate() < 1");
            }
        }
    }
}
